//
// Created by DengLibin on 2019/2/3 0003.
//

#include "ActiveBlock.h"
#include <iostream>
#include <ctime>
#include <iostream>
using namespace std;
ActiveBlock::ActiveBlock() {
    //srand()用来设置rand()产生随机数时的随机数种子，参数seed必须是整数，通常可以用time(0)的返回值作为seed.
    // 如果每次seed都设置相同的值，rand()产生的随机数值每次都一样
    srand(time(0));
    init();
}
void ActiveBlock::init() {
     posX = game_area_width / 2 - 2 * block_size;
     posY = 0 - 4 * block_size;
     currentChangeType=1;
     initZero();
    this->type = getRandomType();
    switch (type){
        //正方形
        case 1:
        {
            arr[2][0] = 1;
            arr[2][1] = 1;
            arr[3][0] = 1;
            arr[3][1] = 1;
            break;
        }
        //L形
        case 2:
        {
            arr[1][1]= 1;
            arr[2][1]= 1;
            arr[3][1]= 1;
            arr[3][2]= 1;
            break;
        }
        //z形
        case 3:
        {
            arr[2][0]=1;
            arr[2][1]=1;
            arr[3][1]=1;
            arr[3][2]=1;
            break;
        }
        //倒T形
        case 4:
        {
            arr[2][2]=1;
            arr[3][1]=1;
            arr[3][2]=1;
            arr[3][3]=1;
            break;
        }
        //竖形
        case 5:
        {
            arr[0][1]=1;
            arr[1][1]=1;
            arr[2][1]=1;
            arr[3][1]=1;
            break;
        }

    }
}
//变形
void ActiveBlock::change() {
    initZero();
    currentChangeType +=1;
    if(currentChangeType == 5){
        currentChangeType = 1;
    }
    switch (type){
        //正方形
        case 1:
        {
            arr[2][0] = 1;
            arr[2][1] = 1;
            arr[3][0] = 1;
            arr[3][1] = 1;
            break;
        }
            //L形
        case 2:
        {
            if(currentChangeType == 1){
                arr[1][1]= 1;
                arr[2][1]= 1;
                arr[3][1]= 1;
                arr[3][2]= 1;
            }else if(currentChangeType == 2){
                arr[2][0]= 1;
                arr[2][1]= 1;
                arr[2][2]= 1;
                arr[3][0]= 1;
            }else if(currentChangeType == 3){
                arr[1][1]= 1;
                arr[2][1]= 1;
                arr[3][1]= 1;
                arr[1][0]= 1;
            } else if(currentChangeType == 4){
                arr[3][0]= 1;
                arr[3][1]= 1;
                arr[3][2]= 1;
                arr[2][2]= 1;
            }
            if(posX < 0){
                posX+=block_size;
            }
            break;
        }
            //z形
        case 3:
        {
            if(currentChangeType == 1 || currentChangeType == 3){
                arr[2][0]=1;
                arr[2][1]=1;
                arr[3][1]=1;
                arr[3][2]=1;
            } else  if(currentChangeType == 2 || currentChangeType == 4){
                arr[1][2]=1;
                arr[2][1]=1;
                arr[2][2]=1;
                arr[3][1]=1;
            }
            if(posX < 0){
                posX+=block_size;
            }

            break;
        }
            //倒T形
        case 4:
        {
            if(currentChangeType == 1){
                arr[2][1]=1;
                arr[3][0]=1;
                arr[3][1]=1;
                arr[3][2]=1;
            }else if(currentChangeType == 2){
                arr[1][1]=1;
                arr[2][1]=1;
                arr[3][1]=1;
                arr[2][2]=1;
            }else if(currentChangeType == 3){
                arr[2][0]=1;
                arr[2][1]=1;
                arr[2][2]=1;
                arr[3][1]=1;
            } else if(currentChangeType == 4){
                arr[1][1]=1;
                arr[2][1]=1;
                arr[2][0]=1;
                arr[3][1]=1;
            }
            if(posX < 0){
                posX+=block_size;
            }

            break;
        }
        //竖形
        case 5:
        {
            if(currentChangeType == 1 || currentChangeType == 3){
                arr[0][1]=1;
                arr[1][1]=1;
                arr[2][1]=1;
                arr[3][1]=1;
            }else if(currentChangeType == 2 || currentChangeType == 4){
                arr[3][0]=1;
                arr[3][1]=1;
                arr[3][2]=1;
                arr[3][3]=1;
                if(posX + 4 * block_size > game_area_width){
                    posX -= (posX+4*block_size - game_area_width);
                }
                if(posX<0){
                    posX += block_size;
                }
            }
            break;
        }
    }
}
int ActiveBlock::getRandomType() {
    //产生一个1-5的随机数
    int randomType = rand() % 5 + 1;
    //return 5;
    return  randomType;
}
void ActiveBlock::initZero() {
    for(int i =0; i < 4; i++){ //行
        for(int j = 0; j < 4; j++){ //列
            arr[i][j] = 0;
        }
    }
}